﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.ComponentModel;
using System.Collections;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;

namespace IronSharePoint.Workflow.Activities
{
    public class IronActivity : Activity
    {
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(IronActivity));
        public static DependencyProperty __ListIdProperty = DependencyProperty.Register("__ListId", typeof(string), typeof(IronActivity));
        public static DependencyProperty __ListItemProperty = DependencyProperty.Register("__ListItem", typeof(int), typeof(IronActivity));
        public static DependencyProperty Variable1Property = DependencyProperty.Register("Variable1", typeof(object), typeof(IronActivity));
        public static DependencyProperty Variable2Property = DependencyProperty.Register("Variable2", typeof(object), typeof(IronActivity));

        private string valueType1;
        private string valueType2;

        [ValidationOption(ValidationOption.Required)]
        public WorkflowContext __Context
        {
            get
            {
                return (WorkflowContext)base.GetValue(__ContextProperty);
            }
            set
            {
                base.SetValue(__ContextProperty, value);
            }
        }


        [ValidationOption(ValidationOption.Required)]
        public string __ListId
        {
            get
            {
                return (string)base.GetValue(__ListIdProperty);
            }
            set
            {
                base.SetValue(__ListIdProperty, value);
            }
        }

        [ValidationOption(ValidationOption.Required)]
        public int __ListItem
        {
            get
            {
                return (int)base.GetValue(__ListItemProperty);
            }
            set
            {
                base.SetValue(__ListItemProperty, value);
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public string Variable1
        {
            get
            {
                return (string)base.GetValue(Variable1Property);
            }
            set
            {
                base.SetValue(Variable1Property, value);
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public string Variable2
        {
            get
            {
                return (string)base.GetValue(Variable2Property);
            }
            set
            {
                base.SetValue(Variable2Property, value);
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public string ValueType1
        {
            get
            {
                return this.valueType1;
            }
            set
            {
                if (!base.DesignMode)
                {
                    throw new InvalidOperationException();
                }
                this.valueType1 = value;
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public string ValueType2
        {
            get
            {
                return this.valueType2;
            }
            set
            {
                if (!base.DesignMode)
                {
                    throw new InvalidOperationException();
                }
                this.valueType2 = value;
            }
        }


        public string ScriptPath
        {
            get { return (string)GetValue(ScriptPathProperty); }
            set { SetValue(ScriptPathProperty, value); }
        }

        public static readonly DependencyProperty ScriptPathProperty =
            DependencyProperty.Register("ScriptPath", typeof(string), typeof(IronActivity));

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            IronScriptEngine engine = new IronScriptEngine();

            IronScriptResolver resovler = new IronScriptResolver();
            resovler.ScriptPath = ScriptPath;
            engine.ScriptResolver = resovler;

            engine.CompileScriptFrom(resovler.ScriptPath, typeof(Activity).Assembly);

            engine.Scope.SetVariable("self", this);

            SPSite site = __Context.Site;
            SPWeb web = __Context.Web;
            SPList list = web.Lists[new Guid(__ListId)];
            SPListItem item = list.GetItemById(__ListItem);

            if (engine.Scope.ContainsVariable("Execute"))
            {
                engine.Scope.SetVariable("site", site);
                engine.Scope.SetVariable("web", web);
                engine.Scope.SetVariable("list", list);
                engine.Scope.SetVariable("item", item);

                object method = engine.Scope.GetVariable("Execute");
                return (ActivityExecutionStatus)engine.Operations.Call(method, executionContext);
            }

            return base.Execute(executionContext);
        }
    }
}
